package com.aat.dxfy.db;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Hello world!
 *
 */
public class SqlServerDbCheck {

	public static void writeStrNextLine(String fileName, String str) {
		try {
			String pathname = System.currentTimeMillis() + ".txt";
			File file = new File(fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
			BufferedReader br = new BufferedReader(new FileReader(file));
			File temp = new File(pathname);
			BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
			String s = null;
			while ((s = br.readLine()) != null) {
				bw.write(s);
				bw.newLine();
			}
			br.close();
			bw.flush();
			char[] chs = str.toCharArray();
			for (int j = 0; j < chs.length; j++) {
				if (chs[j] == '\n' || chs[j] == '\r')
					bw.newLine();
				bw.write(chs[j] + "");
			}
			bw.flush();
			bw.close();
			file.delete();
			temp.renameTo(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection conn = DriverManager.getConnection("jdbc:sqlserver://192.168.114.5:1433; DatabaseName=BJXCRL",
					"sa", "123456");
			//
			Statement sta = conn.createStatement();
			ResultSet rs = sta.executeQuery("SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'  ORDER BY NAME ASC");
			ResultSet rsa=null;
			while (rs.next()) {
				String tn = rs.getString("NAME");
				rsa = conn.createStatement().executeQuery("select count(*) as num1 from " + tn);
				rsa.next();
				int num1 = rsa.getInt("num1");
				if (num1 > 50 && num1 < 300) {
					writeStrNextLine("E:\\db.txt", tn +"  " +num1);
				}
			}
			System.out.println("over");
			rsa.close();
			rs.close();
			//
			conn.close();
		} catch (Exception e) {
		}
	}
}
